<template>
  <div>
    <div class="footer">
      <div class="gotop el-icon-upload2" v-show="gotop" @click="toTop"></div>
    </div>
  </div>
</template>

<script>
export default {
  mounted() {
    // 此处true需要加上，不加滚动事件可能绑定不成功
    window.addEventListener("scroll", this.handleScroll, true);
  },
  methods: {
    handleScroll() {
      let scrolltop =
        document.documentElement.scrollTop || document.body.scrollTop;
      scrolltop > 30 ? (this.gotop = true) : (this.gotop = false);
    },
    toTop() {
      let top = document.documentElement.scrollTop || document.body.scrollTop;
      // 实现滚动效果
      const timeTop = setInterval(() => {
        document.body.scrollTop =
          document.documentElement.scrollTop =
          top -=
            50;
        if (top <= 0) {
          clearInterval(timeTop);
        }
      }, 10);
    },
  },
  data() {
    return {
      gotop: false,
    };
  },
};
</script>

<style scoped>
.footer .gotop {
  text-align: center;
  position: fixed;
  right: 6vw;
  bottom: 10vh;
  cursor: pointer;
  padding: 2.4vw;
  border-radius: 1vw;
  background: #666;
  color: #fff;
}
.el-icon-upload2{
font-size: 5vw;
}
</style>